<%@ page language="java" pageEncoding="UTF-8"%>
 <script>
	var _webApp = '${base}';
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!--
<link href="http://tk-images.oss-cn-hangzhou.aliyuncs.com/mobiel%2Fcss%2Fmemo%2Fmini.css" rel="stylesheet" type="text/css">
-->
<link rel="stylesheet" type="text/css" href="${base}/css/plugin/kindeditor/4.1.10/themes/default/default.css">
<script type="text/javascript" src="${base}/js/plugin/kindeditor/4.1.10/kindeditor-all-min.js"></script>
<script type="text/javascript" src="${base}/js/plugin/kindeditor/4.1.10/lang/zh_CN.js"></script>


<style>

    .cash-table input[type=text]{
        width:80px;
        color:#999999;
    }
    .cash-table .sinput[type=text]{
        width:50px;
        color:#999999;
    }
    .cash-table {
    	width: 950px;
    	padding-top: 14px;
	}
    .button-success,
        .button-error,
        .button-warning,
        .button-secondary {
            color: white;
            border-radius: 4px;
            text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
    }
    
    .button-success {
            background: rgb(28, 184, 65); /* this is a green */
        }

        .button-error {
            background: rgb(202, 60, 60); /* this is a maroon */
        }

        .button-warning {
            background: rgb(223, 117, 20); /* this is an orange */
        }

        .button-secondary {
            background: rgb(66, 184, 221); /* this is a light blue */
        }
        
        th,td{
		    line-height: 18px;
		    /*padding:3px 5px;*/
		    /*vertical-align: baseline;*/
		}
		 
</style>

<script>
var setting = {};
function createDet(){
	KindEditor.ready(function(K) {
		editor = K.editor({
			uploadJson : _webApp + '/file/upload'
		});
	}); 
	initFrameUI();
 	if(setting.add==null){
 		jiben();
 	}else{
 		setting.base.data = {};
 		initBase();
 		clearForm();
 	}

}

function initFrameUI(){
	var html = "";
	 
 	html += '<div class="item-head blue-title" style="margin-top: 5px;"><span class="hsize">'+setting.base.title+'</span></div><form class="pure-form">';
 	html += '<table style="width:100%;margin-top: 10px;font-size: 13px" id="crudDialog"> </table>';
 	html += '</form>';
 	html += '<div id="btnContainer">';
 	if(setting.backBtn!=null && setting.backBtn !=false){
 		html += '<button class="button-secondary pure-button" style="margin-right:10px;width: 100px;" onclick="goBack()">返回</button></div>'	
 	}

 	$('#'+setting.body).html(html);
}

function save(func){
	var saveData = bindOrGetData();
	//required : true
	var cols = setting.base.column;
	var reqs = [];
	for(var i = 0;i<cols.length;i++){
		if(cols[i].required!=null && cols[i].required){
			var value = saveData[cols[i].field];
			if(value == null|| value ==""){
				$.messager.show({
		            title:'提示',
		            msg:cols[i].title+" 不能为空哦 ",
		            showType:'slide',
		            timeout:500,
		            style:{
		                right:'',
		                top:document.body.scrollTop+document.documentElement.scrollTop,
		                bottom:''
		            }
		        });
				return ;
			}
		}
	}
	if(setting.add!=null){
		setting.edit.url = setting.add.url; 
	}
	$.ajax({
		type:'post',
		url:setting.edit.url,
		data:saveData,
		dataType:'json',
		success:function(res){
			if(setting.add!=null && setting.add.callback!=null){
				setting.add.callback(res);
				return;
			}
            alert(res.msg);
			if(func!=null){
                 func();
            }else{
                window.location.reload();
            }
		}
	});
}

function jiben(){
	$.ajax({
		type:'post',
		url:setting.url,
		dataType:'json',
		async:false,
		success:function(res){
			var base = res.data;
			if(base instanceof Array){
				base = base[0];
			}
			setting.base.data = base;
            if(setting.success!=null){
                setting.success(setting.base.data);
            }
			initBase();
		}
	})
}  
function initBase(){
	var col = setting.base.column;
	var base = setting.base.data;
	var html = "";
	for(var j = 1;j<99;j++){
		var rows = [];
		var tr = "<tr>";
		for(var i =0 ;i<col.length;i++){
			if(col[i].pos==null){continue;}
			if(col[i].pos[0]==j){
				col[i].posY = col[i].pos[1];
				col[i].posZ = col[i].pos[2];
				rows.push(col[i]);
			}
		}
		if(rows.length==0){
			break;
		}
		rows = rows.sorted('posY','asc');
		for(var i = 0 ;i<rows.length;i++){
			var title = rows[i].title;
			var field = rows[i].field;
			tr += '<td>'+title+'</td>'+'<td colspan="'+rows[i].posZ+'" >'+ getInputHtml(rows[i],"99.9%")+'</td>';
		}
		tr += "</tr>";
		html += tr;
	}
	for(var i =0 ;i<col.length;i++){
		if(col[i].pos==null){
			html += '<input type="hidden" id="crud-'+col[i].field+'" name="'+col[i].field+'" value="'+base[col[i].field]+'"/>';
		}
	}
	$('#crudDialog').html(html);
	initCrudDialog(setting.base.column);
	if(setting.add==null){
		bindOrGetData(setting.base.data);		
	}

}
function goBack(){
	window.history.go(-1);
} 



function imgInit($this,cb) {
    $this.parent().height(70);
    $this.attr('style',"");
    $this.parent().append('<img src="" onclick="showPic(this)" style="width: 70px;height: 70px;"/>');
    $this.change(function(){
        $this.parent().find('img').attr('src',$this.val());
    });
    $this.change();

    $this.click(function(){
        editor.loadPlugin('image', function() {
            editor.plugin.imageDialog({
                showRemote : false,
                clickFn : function(url, title, width, height, border, align) {
                    $this.val(url);
                    $this.change();
                    if(cb!=null){
                        cb(url);
                    }
                    editor.hideDialog();
                }
            });
        });
        $('.ke-dialog').css('top','100px');
    });
}
function initCrud(){
	 
	$('#crudDialog input').each(function(){
		var $this = $(this);
		var type = $this.data('type');
		if(type == 'img'){
            imgInit($this);
		}
		if(type == 'date'){
			$this.datetimepicker({
				lang:'ch', 
				format:'Y-m-d H:i',
			});
		}
		if(type == 'number'){
			$this.keyup(function(){  
	            var c=$(this);  
	            if(/[^\d]/.test(c.val())){//替换非数字字符  
	              var temp_amount=c.val().replace(/[^\d]/g,'');  
	              $(this).val(temp_amount);  
	            }  
	         })   
		}
	});
}


function showPic(row){  
	var src = $(row).attr('src'); 
	window.top.showPic(src);
}

</script>
 